<?php
 /*****************************************************************************
 * newInvoice.php                                                             *
 * Last Modified: 2007-06-06                                                  *
 *                                                                            *
 * newInvoice.php outputs an HTML form to be used to create a new invoice     *
 * record.                                                                    *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      newInvoice.php                                                  *
 * @version   1.0.0                                                           *
 * @uses      Jpmaster77's Login Script (Written by: Jpmaster77, 2004-08-19)  *
 *             ->(http://www.evolt.org/PHP-Login-System-with-Admin-Features/) *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/
 
 
  // Require session.php for user access control
	require "include/session.php";

  // If user is not logged in, redirect to main.php
	if(!$session->logged_in){
		header("Location: main.php");
		exit();
	}

  // Require config.inc.php for configuration variables
	require_once "config.inc.php";

  // Connect to MySQL
	mysql_connect($path, $username, $password);
	// Select the database, if error die with error message
	@mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

  // Define MySQL query to retrieve all of the contacts
	$query="SELECT * FROM tblContacts ORDER BY lastName, firstName, customerID ASC";

  // Execute MySQL query, if error die with error message
	$result = mysql_query($query) or die(mysql_error());

  // Determine the number of rows returned
	$num = mysql_numrows($result);

  // Close the connection
	mysql_close();

  // Output the POST form for creating a new invoice
	echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
?>

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta name="ROBOTS" content="NOODP, NOFOLLOW" />
		<meta name="GOOGLEBOT" content="NOODP, NOFOLLOW" />
		<title>Bugs Bee Gone - Billing - Create New Invoice</title>
		<meta name="description" content="Bugs Bee Gone - Billing - Create New Invoice" />
		<meta name="keywords" content="php, mysql, test, practice" />
		<link rel="shortcut icon" href="" />
		<link href="contact.css" type="text/css" rel="stylesheet" />

		<script type="text/javascript" src="func.inc.js"></script>
	</head>

	<body>

	<div style="text-align: center;">
	<p>Create a New Invoice:</p>
	<form action="createInvoice.php" method="post">
		<table width="600px" align="center" cellspacing="4" cellpadding="2" style="border-style:solid;border-width:1px;border-color:black;">
		<tr>
			<td valign="top" width="150px">
				<b>Customer ID:</b>
			</td>

			<td valign="top" colspan="2">
				<select name="customerID">
					<option value=""></option>
					<?php
            // Declare the incrementor and initialize to 0
						$j = 0;
						
						// Cycle through loop while $j is less than $num
						while($j < $num) {
              // Define data variables
							$customerID = mysql_result($result,$j,"customerID");
							$company = mysql_result($result,$j,"company");
							$firstName = mysql_result($result,$j,"firstName");
							$lastName = mysql_result($result,$j,"lastName");

              // If company != NULL...
							if($company != "") {
                // Output company name then the last name and first name
								echo '<option value="' . $customerID . '">' . $company . ' - ' . $lastName . ', ' . $firstName . '</option>' . "\n";
              // If the company is NULL...
							} else {
                // Output the last name and first name
								echo '<option value="' . $customerID . '">' . $lastName . ', ' . $firstName . '</option>' . "\n";
							}

              // Increment the incrementor by 1
							$j++;
						}  // End while($j < $num)
					?>
				</select>
			</td>
		</tr>

		<?php
      // Declare incrementor and initialize to 0
			$i=1;
			
			// Loop through while $i is less than 11
			// giving us 10 line item object
			while($i < 11) {
        // Output the line item objects
				echo '<tr>' . "\n";
				echo '<td valign="top" colspan="3" style="background-color:#F8F8FF;">' . "\n";
					echo '<b><u>Item ' . $i . '</u></b>' . "\n";
				echo '</td>' . "\n";
				echo '</tr>' . "\n";

				echo '<tr>' . "\n";
				echo '<td valign="top" width="150px">' . "\n";
						echo '<b>Qty</b>' . "\n";
				echo '</td>' . "\n";

				echo '<td valign="top" width="150px">' . "\n";
						echo '<b>Name</b>' . "\n";
				echo '</td>' . "\n";

				echo '<td valign="top" width="150px">' . "\n";
						echo '<b>Rate</b>' . "\n";
				echo '</td>' . "\n";
				echo '</tr>' . "\n";

				echo '<tr>' . "\n";
				echo '<td valign="top" width="150px">' . "\n";
						echo '<input type="text" name="item' . $i . 'Qty" class="required" />' . "\n";
				echo '</td>' . "\n";

				echo '<td valign="top" width="150px">' . "\n";
						echo '<textarea rows="3" cols="20" name="item' . $i . 'Name" class="required" onkeypress="return checkMaxLength(event,this)" TAMaxLength="100"></textarea>' . "\n";
				echo '</td>' . "\n";

				echo '<td valign="top" width="175px">' . "\n";
						echo '$ <input type="text" name="item' . $i . 'Rate" class="required" />' . "\n";
				echo '</td>' . "\n";
				echo '</tr>' . "\n";

        // Increment the incrementor by 1
				$i++;
			}  //End while($i < 11)
		?>
		<tr>
			<td valign="top" width="150px">
				<b>Discount:</b>
			</td>

			<td valign="top" colspan="2">
				$ <input type="text" name="discount" />
			</td>
		</tr>

		<tr>
			<td valign="top" width="150px">
				<b>Tax Rate:</b>
			</td>

			<td valign="top" colspan="2">
				<input type="text" name="taxRate" />%
			</td>
		</tr>

		<tr>
			<td valign="top" width="150px">
				<b>Notes:</b>
			</td>

			<td valign="top" colspan="2">
				<textarea rows="4" cols="40" name="notes" onkeypress="return checkMaxLength(event,this)" TAMaxLength="400"></textarea>
			</td>
		</tr>
		</table>

		<input type="Submit" value="Create Invoice" /> <input type="button" onclick="cancel(this)" location="invoices.php" value="Cancel" />
	</form>
	</div>

	</body>
</html>
